package com.yyk.yykywd.ui.work;

import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yykywd.R;
import com.yyk.yykywd.base.BaseActivity;
import com.yyk.yykywd.base.BaseRecyclerAdapter;
import com.yyk.yykywd.base.BaseRecyclerHolder;
import com.yyk.yykywd.bean.WorkPlanlistInfo;
import com.yyk.yykywd.http.ApiService;
import com.yyk.yykywd.listenter.ToolbarListenter;
import com.yyk.yykywd.ui.MainActivity;
import com.yyk.yykywd.util.Md5Util2;
import com.yyk.yykywd.widght.diver.CustomDecoration;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 拜访客户
 */
public class CustomerCallActivity extends BaseActivity {
    public static CustomerCallActivity intance;

    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.ll_emptyview)
    LinearLayout llEmptyview;
    @BindView(R.id.ll_list)
    LinearLayout llList;
    private BaseRecyclerAdapter<WorkPlanlistInfo.DataBean> adapter;
    private List<WorkPlanlistInfo.DataBean> list = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_customer_call;
    }

    @Override
    public void initView() {
        initToolbar();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        startAct(MainActivity.class, true);
    }

    private void initToolbar() {
        setBackArrow(new ToolbarListenter() {
            @Override
            public void onClickListenter() {
                startAct(MainActivity.class, true);

            }
        });
        setTitle("客户拜访");
    }

    @Override
    public void initData() {
        intance = this;
        initAdapter();
    }


    @Override
    protected void onResume() {
        super.onResume();
        postWorkPlanlist(true);
    }

    private void initAdapter() {

        adapter = new BaseRecyclerAdapter<WorkPlanlistInfo.DataBean>(mActivity, list, R.layout.adapter_item_customercall) {
            @Override
            public void convert(BaseRecyclerHolder holder, WorkPlanlistInfo.DataBean item, int position, boolean isScrolling) {
                holder.setText(R.id.tv_uname, item.getUname());
                holder.setText(R.id.tv_hname, item.getHname());
                holder.setText(R.id.tv_createtime, item.getCreatetime() + "");
                if (item.getEnd() == 1) {
                    holder.setText(R.id.tv_time_end, item.getTime() + "  上午");
                } else if (item.getEnd() == 2) {
                    holder.setText(R.id.tv_time_end, item.getTime() + "  下午");
                } else {
                    holder.setText(R.id.tv_time_end, item.getTime() + "  晚上");
                }

                holder.setText(R.id.tv_topname, item.getTopname());
                holder.setImageByUrl(R.id.iv_img, item.getImg());
            }
        };
        rvList.addItemDecoration(new CustomDecoration(mActivity, CustomDecoration.VERTICAL_LIST, R.drawable.divider_f8f_10, 0));
        rvList.setLayoutManager(new LinearLayoutManager(mActivity));
        rvList.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView parent, View view, int position) {
                Bundle bundle = new Bundle();
                //员工ID
                //业务编号
                //创建时间
                //业务类型
                //机构名称
                bundle.putString("id", list.get(position).getId() + "");
                startAct(CustomerCall2Activity.class, bundle);
            }
        });

    }

    /**
     * 接口3：业务计划和拜访列表（whm）
     *
     * @param flag
     */
    private void postWorkPlanlist(final boolean flag) {
        Map<String, String> map = new TreeMap<>();
        map.put("uid", Hawk.get("uid") + "");
        map.put("page", 1 + "");
        map.put("limit", 100 + "");
        map.put("del", 1 + ""); //显示

        map.put("sign", Md5Util2.createSign(map));
        ApiService.getInstance().api.postWorkPlanlist(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Observer<WorkPlanlistInfo>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(WorkPlanlistInfo info) {
                        Logger.e("业务计划和拜访列表" + info.toString());
                        if (info.getCode() == 1) {
                            llEmptyview.setVisibility(View.GONE);
                            llList.setVisibility(View.VISIBLE);
                            if (flag) {
                                list.clear();
                            }
                            if (info.getData() != null & info.getData().size() > 0) {
                                list.addAll(info.getData());
//                                llNullBg.setVisibility(View.GONE);
                            }

                            adapter.notifyDataSetChanged();
                        } else if (info.getCode() == 2) {
                            llEmptyview.setVisibility(View.VISIBLE);
                            llList.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
